package com.sinosoft.app.service.imp.filter;

import com.sinosoft.app.service.connector.IFilter;
import com.sinosoft.commontools.record.Record;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter implements IFilter {
	protected static String LOGINUSER = "userid";
	protected static String LOGTICKET = "loginticket";
	protected static String LOGATTR = "passed";
	protected static String PASSVALUE = "true";
	protected static ConcurrentHashMap<String, String> suffix = null;

	public int doFilter(ServletRequest arg0, ServletResponse arg1, Record arg2)
			throws IOException, ServletException {
		byte arg3 = 3;
		HttpServletRequest arg4 = (HttpServletRequest) arg0;
		String arg5 = arg4.getRequestURI();
		if (!this.IsStart(arg5, arg2) && !this.isPassport(arg5, arg2)
				&& !this.IsLoginPage(arg5)) {
			HttpSession arg6 = arg4.getSession();
			if (arg6 != null) {
				boolean arg7 = this.VerifyLoginInfo(arg6);
				if (!arg7) {
					this.OutputErr("您还没有登录，不能进入系统！", arg1);
					arg3 = 1;
					if (arg2.getStrValue("loginstyle").equalsIgnoreCase("local")) {
						HttpServletResponse arg8 = (HttpServletResponse) arg1;
						arg8.sendRedirect(arg2.getStrValue("loginpage"));
					}
				}
			} else {
				if (arg2.getStrValue("loginstyle").equalsIgnoreCase("local")) {
					HttpServletResponse arg8 = (HttpServletResponse) arg1;
					arg8.sendRedirect(arg2.getStrValue("loginpage"));
				}

				arg3 = 1;
			}
		}

		return arg3;
	}

	private boolean isPassport(String arg0, Record arg1) {
		synchronized (this) {
			if (suffix == null) {
				suffix = new ConcurrentHashMap();
				String arg3 = arg1.getStrValue("passinfo");
				StringTokenizer arg4 = new StringTokenizer(arg3, ",");

				while (arg4.hasMoreTokens()) {
					String arg5 = arg4.nextToken();
					suffix.put(arg5, "");
				}
			}
		}

		return this.ConfigFilter(arg0);
	}

	private boolean ConfigFilter(String arg0) {
		String arg1 = arg0.toLowerCase();
		Iterator arg3 = suffix.keySet().iterator();

		while (arg3.hasNext()) {
			String arg2 = (String) arg3.next();
			if (arg1.endsWith(arg2.toLowerCase())) {
				return true;
			}
		}

		return false;
	}

	private boolean VerifyLoginInfo(HttpSession arg0) {
		boolean arg1 = false;
		Object arg2 = arg0.getAttribute(LOGINUSER);
		Object arg3 = arg0.getAttribute(LOGTICKET);
		Object arg4 = arg0.getAttribute(LOGATTR);
		String arg5 = arg2 == null ? "" : arg2.toString();
		String arg6 = arg3 == null ? "" : arg3.toString();
		String arg7 = arg4 == null ? "" : arg4.toString();
		if (!arg5.equals("") && arg7.equals(PASSVALUE)
				&& this.VerifyLoginTicket(arg6)) {
			arg1 = true;
		}

		return arg1;
	}

	private boolean VerifyLoginTicket(String arg0) {
		return true;
	}

	private void OutputErr(String arg0, ServletResponse arg1)
			throws ServletException, IOException {
		arg1.setContentType("text/html; charset=UTF-8");
		PrintWriter arg2 = arg1.getWriter();
		arg2.println("<script language=\"javascript\">");
		arg2.println("alert(\"" + arg0 + "\");");
		arg2.println("window.close();");
		arg2.println("</script>");
	}

	private boolean IsLoginPage(String arg0) {
		return arg0.toLowerCase().endsWith("login.jsp")
				|| arg0.toLowerCase().endsWith("login.do");
	}

	private boolean IsStart(String arg0, Record arg1) {
		return arg0.equals("/" + arg1.getStrValue("appname") + "/")
				|| arg0.equals("/" + arg1.getStrValue("appname"));
	}
}